package groep3.ssc.helpers;

import java.util.concurrent.TimeUnit;

public class DataFunctions
{

    public static String longToFormattedTime(long time)
    {
        time = Math.abs(time);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(time);
        time -= (minutes * 60 * 1000);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(time);
        time -= (seconds * 1000);
        long hundreths = time / 10;

        if (minutes > 0)
        {
            return String.format("%02d:%02d.%02d", minutes, seconds, hundreths);

        } else
        {
            return String.format("%02d.%02d", seconds, hundreths);

        }
    }

    public static String millisecondsToReadableTime(long millis)
    {
        boolean negative = millis < 0;
        millis = Math.abs(millis);

        long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
        millis = millis - (seconds * 1000);
        String formatted = String.format("%02d.%02d", seconds, millis);

        if (negative)
        {
            return "- " + formatted;
        } else
        {
            return "+ " + formatted;
        }
    }

    public static long FormattedTimeToInt(String time)
    {
        return FormattedTimeToInt(time, "\\.");
    }

    public static long FormattedTimeToInt(String time, String separator)
    {
        long millis = 0;
        String[] splittedTime = time.split(separator);

        if (splittedTime.length > 0)
        {
            int seconds = Integer.parseInt(splittedTime[0]);
            millis += seconds * 1000;
        }

        if (splittedTime.length > 1)
        {
            if(splittedTime[1].length() > 3)
            {
                splittedTime[1] = splittedTime[1].substring(0, 3);
            }
            int milliseconds = Integer.parseInt(splittedTime[1]);
            millis += milliseconds * (Math.pow(10, (3 - splittedTime[1].length())));
        }
        return millis;
    }
}
